#include <string.h>
#include <stdio.h>
#include "mhscpu.h"

#define EXT_ANA_ATTACK_TEST
// #define SOFT_ATTACK_TEST

#define EXT_ATTACK_TEST
#define EXT_ATTACK_ALL_STATIC_HIGH	
//#define EXT_ATTACK_ALL_STATIC_LOW_WITH_PULL_UP	
// #define EXT_ATTACK_ALL_STATIC_LOW_WITHOUT_PULL_UP	
//#define EXT_ATTACK_ALL_DYNAMIC		
// #define EXT_ATTACK_DYNAMIC_AND_STATIC_WITH_PULL_UP
// #define EXT_ATTACK_DYNAMIC_AND_STATIC_WITHOUT_PULL_UP


#define ANA_ATTACK_TEST

#define ANA_ATTACK_VOL_HIGH
#define ANA_ATTACK_VOL_LOW
#define ANA_ATTACK_TEMPER_HIGH
#define ANA_ATTACK_TEMPER_LOW
// #define ANA_ATTACK_MESH 
#define ANA_ATTACK_VOLGLITCH
#define ANA_ATTACK_XTAL32K
// #define ANA_ATTACK_ALL


void NVIC_Configuration(void);
void UART_Configuration(void);
void SENSOR_Configuration(void);

void clearBPK(void);
void setBPK(void);
void printBPK(void);

int main(void)
{
    #ifdef SOFT_ATTACK_TEST 
    uint32_t test_delay = 0xFFFFF;
    #endif
	
	
// 	SYSCTRL_PLLConfig(SYSCTRL_PLL_96MHz);
// 	SYSCTRL_HCLKConfig(SYSCTRL_HCLK_Div1);
// 	SYSCTRL_PCLKConfig(SYSCTRL_PCLK_Div2);
	
	SYSCTRL_APBPeriphClockCmd(SYSCTRL_APBPeriph_UART0 | SYSCTRL_APBPeriph_BPU | SYSCTRL_APBPeriph_GPIO,ENABLE);
	SYSCTRL_APBPeriphResetCmd(SYSCTRL_APBPeriph_UART0 | SYSCTRL_APBPeriph_BPU,ENABLE);
	
	
//	SSC->SSC_CR1 = 0x00;
	
	SENSOR_ANACmd(SENSOR_ANA_MESH, DISABLE);
	UART_Configuration();
//     #ifdef EXT_ANA_ATTACK_TEST
 		SENSOR_Configuration();
//     #endif
//     #ifdef SOFT_ATTACK_TEST 
// 		SENSOR_Soft_Enable();
//     #endif
	NVIC_Configuration();
	
	
	printf("MegaHunt SCPU BPK Demo V1.0.\r\n");
	
    #ifdef SOFT_ATTACK_TEST     
		 /* Test Soft attack */
		SENSOR_SoftAttack();
		while (test_delay--);
		SENSOR_SoftAttack();
    #endif
	
	while(1)
	{

	}

}

void SENSOR_Configuration(void)
{
	SENSOR_EXTInitTypeDef SNESOR_EXTInitStruct;
	memset(&SNESOR_EXTInitStruct, 0, sizeof(SNESOR_EXTInitStruct));

#ifdef EXT_ATTACK_ALL_STATIC_LOW_WITHOUT_PULL_UP
	SNESOR_EXTInitStruct.SENSOR_Port_StaticLowEffect = SENSOR_Port_S0| SENSOR_Port_S1|
														SENSOR_Port_S2| SENSOR_Port_S3;
														//SENSOR_Port_S4| SENSOR_Port_S5;
#endif

#ifdef EXT_ATTACK_ALL_STATIC_LOW_WITH_PULL_UP
	SNESOR_EXTInitStruct.SENSOR_Port_StaticLowEffect = SENSOR_Port_S0| SENSOR_Port_S1|
														SENSOR_Port_S2| SENSOR_Port_S3;
														//SENSOR_Port_S4| SENSOR_Port_S5;
	SNESOR_EXTInitStruct.SENSOR_Port_PullUp = SNESOR_EXTInitStruct.SENSOR_Port_StaticLowEffect;
#endif

#ifdef EXT_ATTACK_ALL_STATIC_HIGH
	SNESOR_EXTInitStruct.SENSOR_Port_StaticHighEffect = SENSOR_Port_S0| SENSOR_Port_S1|
														SENSOR_Port_S2| SENSOR_Port_S3;
														//SENSOR_Port_S4| SENSOR_Port_S5;
	SNESOR_EXTInitStruct.SENSOR_Port_PullUp = SNESOR_EXTInitStruct.SENSOR_Port_StaticHighEffect;
#endif


#ifdef EXT_ATTACK_ALL_DYNAMIC		
	SNESOR_EXTInitStruct.SENSOR_Port_Dynamic = 			SENSOR_Port_S0| SENSOR_Port_S1|
														SENSOR_Port_S2| SENSOR_Port_S3;
														//SENSOR_Port_S4| SENSOR_Port_S5|
														//SENSOR_Port_S6| SENSOR_Port_S7|
														//SENSOR_Port_S8| SENSOR_Port_S9|
														//SENSOR_Port_S10| SENSOR_Port_S11;
#endif

#ifdef EXT_ATTACK_DYNAMIC_AND_STATIC_WITH_PULL_UP
	SNESOR_EXTInitStruct.SENSOR_Port_StaticLowEffect =  SENSOR_Port_S0| SENSOR_Port_S1|
														SENSOR_Port_S2| SENSOR_Port_S3;
	SNESOR_EXTInitStruct.SENSOR_Port_StaticHighEffect =	SENSOR_Port_S4| SENSOR_Port_S5|
														SENSOR_Port_S6| SENSOR_Port_S7;
	SNESOR_EXTInitStruct.SENSOR_Port_Dynamic =  		SENSOR_Port_S8| SENSOR_Port_S9|
														SENSOR_Port_S10| SENSOR_Port_S11;
	SNESOR_EXTInitStruct.SENSOR_Port_PullUp = SNESOR_EXTInitStruct.SENSOR_Port_StaticLowEffect;
#endif

#ifdef EXT_ATTACK_DYNAMIC_AND_STATIC_WITHOUT_PULL_UP
	SNESOR_EXTInitStruct.SENSOR_Port_StaticLowEffect =  SENSOR_Port_S0| SENSOR_Port_S1|
														SENSOR_Port_S2| SENSOR_Port_S3;
	SNESOR_EXTInitStruct.SENSOR_Port_StaticHighEffect =	SENSOR_Port_S4| SENSOR_Port_S5|
														SENSOR_Port_S6| SENSOR_Port_S7;
	SNESOR_EXTInitStruct.SENSOR_Port_Dynamic =  		SENSOR_Port_S8| SENSOR_Port_S9|
														SENSOR_Port_S10| SENSOR_Port_S11;
#endif

	SNESOR_EXTInitStruct.SENSOR_DynamicFrequency = SENSOR_DynamicFrequency_1s;

	SENSOR_AttackRespMode(SENSOR_Interrupt);

#ifdef EXT_ATTACK_TEST
    SNESOR_EXTInitStruct.SENSOR_Port_Enable = ENABLE;
	SENSOR_EXTCmd(DISABLE);
	while(SET == SENSOR_EXTIsRuning());
	SENSOR_EXTInit(&SNESOR_EXTInitStruct);
	SENSOR_EXTCmd(ENABLE);
#endif

#ifdef ANA_ATTACK_TEST

#ifdef ANA_ATTACK_ALL
	SENSOR_ANACmd(SENSOR_ANA_VOL_HIGH|
						SENSOR_ANA_VOL_LOW|
						SENSOR_ANA_TEMPER_HIGH|
						SENSOR_ANA_TEMPER_LOW|
						SENSOR_ANA_XTAL32K |
						SENSOR_ANA_MESH |
						SENSOR_ANA_VOLGLITCH , ENABLE);
#else
	SENSOR_ANACmd(SENSOR_ANA_VOL_HIGH|
						SENSOR_ANA_VOL_LOW|
						SENSOR_ANA_TEMPER_HIGH|
						SENSOR_ANA_TEMPER_LOW|
						SENSOR_ANA_XTAL32K |
						SENSOR_ANA_MESH |
						SENSOR_ANA_VOLGLITCH , DISABLE);
#endif

#ifdef ANA_ATTACK_VOL_HIGH
	SENSOR_ANACmd(SENSOR_ANA_VOL_HIGH, ENABLE);
#else
	SENSOR_ANACmd(SENSOR_ANA_VOL_HIGH, DISABLE);
#endif
#ifdef ANA_ATTACK_VOL_LOW
	SENSOR_ANACmd(SENSOR_ANA_VOL_LOW, ENABLE);
#else
	SENSOR_ANACmd(SENSOR_ANA_VOL_LOW, DISABLE);
#endif
#ifdef ANA_ATTACK_TEMPER_HIGH
	SENSOR_ANACmd(SENSOR_ANA_TEMPER_HIGH, ENABLE);
#else
	SENSOR_ANACmd(SENSOR_ANA_TEMPER_HIGH, DISABLE);
#endif
#ifdef ANA_ATTACK_TEMPER_LOW
	SENSOR_ANACmd(SENSOR_ANA_TEMPER_LOW, ENABLE);
#else
	SENSOR_ANACmd(SENSOR_ANA_TEMPER_LOW, DISABLE);
#endif
#ifdef ANA_ATTACK_MESH
	SENSOR_ANACmd(SENSOR_ANA_MESH, ENABLE);
#else
	SENSOR_ANACmd(SENSOR_ANA_MESH, DISABLE);
#endif
#ifdef ANA_ATTACK_VOLGLITCH
	SENSOR_ANACmd(SENSOR_ANA_VOLGLITCH, ENABLE);
#else
	SENSOR_ANACmd(SENSOR_ANA_VOLGLITCH, DISABLE);
#endif
#ifdef ANA_ATTACK_XTAL32K
	SENSOR_ANACmd(SENSOR_ANA_XTAL32K, ENABLE);
#else
	SENSOR_ANACmd(SENSOR_ANA_XTAL32K, DISABLE);
#endif
				
#endif

}


void clearBPK(void)
{
	uint32_t buf[32];
	uint32_t index = 0;
	
	while(BPK_IsReady() == RESET);
	
	//clear buf
	memset(buf, 0, sizeof(buf));

	printf("bufValue:\n");
	for(index = 0;index < sizeof(buf)/sizeof(buf[0]);index++)
	{
		printf("%08X ", buf[index]);
		if (3 == index % 4)
		{
			printf("\n");
		}
	}
	printf("\n");
	BPK_WriteKey(buf,sizeof(buf)/sizeof(buf[0]),0);
}

void setBPK(void)
{
	uint32_t buf[32];
	uint32_t index = 0;
	
	while(BPK_IsReady() == RESET);
	
	//set buf 0~X
	for(index = 0;index < sizeof(buf)/sizeof(buf[0]);index++)
	{
		buf[index] = index;
	}

	printf("bufValue:\n");
	for(index = 0;index < sizeof(buf)/sizeof(buf[0]);index++)
	{
		printf("%08X ", buf[index]);
		if (3 == index % 4)
		{
			printf("\n");
		}
	}
	printf("\n");
	BPK_WriteKey(buf,sizeof(buf)/sizeof(buf[0]),0);
}

void printBPK(void)
{
	uint32_t key[32];
	uint32_t index = 0;
	
	while(BPK_IsReady() == RESET);

	printf("readKey:\n");
	memset(key, 0, sizeof(key));
	BPK_ReadKey(key,sizeof(key)/sizeof(key[0]),0);
	for(index = 0;index < sizeof(key)/sizeof(key[0]);index++)
	{
		printf("%08X ", key[index]);
		if (3 == index % 4)
		{
			printf("\n");
		}
	}
	printf("\n");
	memset(key, 0, sizeof(key));
}


void SENSOR_IRQHandler(void)
{
	printf("SENSOR_IRQHandler in %08X\n",SENSOR->SEN_STATE);
	SENSOR->SEN_STATE = 0;

	printBPK();
	setBPK();
	printBPK();
	
	NVIC_ClearPendingIRQ(SENSOR_IRQn);
}

void SSC_IRQHandler(void)
{
	printf("SSC_IRQHandler in %08X\n",SSC->SSC_SR);
	SSC_ClearITPendingBit(SSC_ITSysXTAL12M|SSC_ITSysGlitch|SSC_ITSysVolHigh|SSC_ITSysVolLow);

	printBPK();
	setBPK();
	printBPK();
	
	NVIC_ClearPendingIRQ(SENSOR_IRQn);
}


void NVIC_Configuration(void)
{
	NVIC_InitTypeDef NVIC_InitStructure;

	NVIC_SetPriorityGrouping(NVIC_PriorityGroup_3);
	
	NVIC_InitStructure.NVIC_IRQChannel = SENSOR_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
	
	NVIC_InitStructure.NVIC_IRQChannel = SSC_IRQn;
	NVIC_Init(&NVIC_InitStructure);
}

void UART_Configuration(void)
{
	UART_InitTypeDef UART_InitStructure;
	
	GPIO_PinRemapConfig(GPIOA, GPIO_Pin_0 | GPIO_Pin_1, GPIO_Remap_0);
	
	UART_InitStructure.UART_BaudRate = 115200;
	UART_InitStructure.UART_WordLength = UART_WordLength_8b;
	UART_InitStructure.UART_StopBits = UART_StopBits_1;
	UART_InitStructure.UART_Parity = UART_Parity_No;
	
	UART_Init(UART0, &UART_InitStructure);
//    UART_Init(UART1, &UART_InitStructure);
}


#ifdef  USE_FULL_ASSERT

/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{ 
	/* User can add his own implementation to report the file name and line number,
	 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

	/* Infinite loop */
	while (1)
	{
	}
}
#endif


